/**
 * Created by LC on 2019/11/7
 */
package com.hw.paltfrom.service.doncutor;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.exception.HWSystemException;
import com.hw.paltfrom.mapper.HwCommandWorkflowMapper;
import com.hw.paltfrom.pojo.HwCommandWorkflow;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CommandWorkflowService {

    @Autowired
    private HwCommandWorkflowMapper workflowMapper;

    /**
     * 插入一条新操作流程
     * @param hwCommandWorkflow
     * @return 是否成功
     */
    public boolean addWorkflow(HwCommandWorkflow hwCommandWorkflow){
        Integer insert = workflowMapper.insert(hwCommandWorkflow);
        if (insert == 1){
            return true;
        }
        return false;
    }

    /**
     * 查询某模块流水
     * @return 查询到的操作流程
     */
    public List<HwCommandWorkflow> getCommandWorkflow(String workflowModule,Integer top){
        Wrapper<HwCommandWorkflow> workflowWrapper = new EntityWrapper<>();
        if (top <= 0){
            top = 1;
        }
        String limit = "limit "+top;
        //根据时间倒叙查询前top条数据
        workflowWrapper.eq("WorkFlow_Module",workflowModule)
                .orderBy("CommandWorkFlow_DataTime",false).last(limit);
        List<HwCommandWorkflow> hwCommandWorkflows = workflowMapper.selectList(workflowWrapper);
        return hwCommandWorkflows;
    }

    /**
     * 查询某一业务操作流水
     * @param workflowModule
     * @param objectId
     * @return
     */
    public HwCommandWorkflow getCommandWorkflow(String workflowModule,String objectId){
//        Wrapper<HwCommandWorkflow> workflowWrapper = new EntityWrapper<>();
//        workflowWrapper.eq("WorkFlow_Module",workflowModule).eq("OBJECT_ID",objectId);
        //根据条件查询
        HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
        hwCommandWorkflow.setWorkflowModule(workflowModule);
        hwCommandWorkflow.setObjectId(objectId);

        return workflowMapper.selectOne(hwCommandWorkflow);
    }

    /**
     * 更新操作过程
     * @param hwCommandWorkflow //必须有ObjectId
     * @return 是否成功
     */
    public boolean updateWorkFlow(HwCommandWorkflow hwCommandWorkflow){
        //不传ObjectId返回错误
        if (StringUtils.isBlank(hwCommandWorkflow.getObjectId())){
            return false;
        }
        //根据ObjectId更新
        Wrapper<HwCommandWorkflow> workflowWrapper = new EntityWrapper<>();
        workflowWrapper.eq("OBJECT_ID",hwCommandWorkflow.getObjectId());

        Integer update = workflowMapper.update(hwCommandWorkflow, workflowWrapper);
        //更新失败
        if (update != 1){
            return false;
        }

        return true;
    }
}
